<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-row>
          <a-col :span="24">
            <a-form-model-item label="工单编号" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="workOrderNumber">
              <a-input v-model="model.workOrderNumber" placeholder="请输入工单编号"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="数量" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="quantity">
              <a-input-number v-model="model.quantity" placeholder="请输入数量" style="width: 100%"/>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="工厂" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="factoryId">
              <j-dict-select-tag type="list" v-model="model.factoryId" :dictCode="factoryDictCode" placeholder="请选择工厂"
                                 :use-cache="false" @change="chooseFactory"/>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="车间" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="workshopId">
              <j-dict-select-tag type="list" v-model="model.workshopId" :dictCode="workshopDictCode" placeholder="请选择车间"
                                 :use-cache="false" :disabled="workshopStatus" @change="chooseWorkshop"/>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="产线" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="lineId">
              <j-dict-select-tag type="list" v-model="model.lineId" :dictCode="lineDictCode" placeholder="请选择产线"
                                 :use-cache="false" :disabled="lineStatus"/>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="开始时间" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="startTime">
              <j-date placeholder="请选择开始时间" v-model="model.startTime" :show-time="true"
                      date-format="YYYY-MM-DD HH:mm:ss" style="width: 100%"/>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="结束时间" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="endTime">
              <j-date placeholder="请选择结束时间" v-model="model.endTime" :show-time="true" date-format="YYYY-MM-DD HH:mm:ss"
                      style="width: 100%"/>
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>
    </j-form-container>
  </a-spin>
</template>

<script>

  import { httpAction, getAction } from '@/api/manage'
  import { validateDuplicateValue } from '@/utils/util'

  export default {
    name: 'MesWorkOrderForm',
    components: {},
    props: {
      //表单禁用
      disabled: {
        type: Boolean,
        default: false,
        required: false
      }
    },
    data() {
      return {
        factoryDictCode: 'mes_base_factory,factory_name,id,factory_level=1 and factory_status=1',
        workshopDictCode: 'mes_base_factory,factory_name,id,factory_level=2 and factory_status=1',
        lineDictCode: 'mes_base_factory,factory_name,id,factory_level=3 and factory_status=1',
        workshopStatus: true,
        lineStatus: true,

        model: {},
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 }
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 }
        },
        confirmLoading: false,
        validatorRules: {
          workOrderNumber: [
            { required: true, message: '请输入工单编号!' },
            { pattern: /^[a-zA-Z0-9]{7}$/, message: '请输入7位编码' }
          ],
          quantity: [
            { required: true, message: '请输入数量!' },
            { pattern: /^-?\d+$/, message: '请输入整数!' }
          ]
        },
        url: {
          add: '/mes/mesWorkOrder/add',
          edit: '/mes/mesWorkOrder/edit',
          queryById: '/mes/mesWorkOrder/queryById'
        }
      }
    },
    computed: {
      formDisabled() {
        return this.disabled
      }
    },
    created() {
      //备份model原始值
      this.modelDefault = JSON.parse(JSON.stringify(this.model))

    },
    methods: {
      //自建方法----start
      chooseFactory() {
        if (this.model.factoryId) {
          //设置车间能选择
          this.workshopStatus = false
          this.workshopDictCode = 'mes_base_factory,factory_name,id,factory_level=2 and factory_status=1 and pid=\'' + this.model.factoryId + '\''
        } else {
          //清空车间和产线
          this.model.workshopId = ''
          this.model.lineId = ''
          //设置车间和产线不能用
          this.workshopStatus = true
          this.lineStatus = true
        }
      },
      chooseWorkshop() {
        if (this.model.workshopId) {
          //设置产线能选择
          this.lineStatus = false
          this.lineDictCode = 'mes_base_factory,factory_name,id,factory_level=3 and factory_status=1 and pid=\'' + this.model.workshopId + '\''
        } else {
          //清空产线
          this.model.lineId = ''
          //设置产线不能用
          this.lineStatus = true
        }
      },
      //自建方法------end

      add() {
        this.edit(this.modelDefault)
      },
      edit(record) {
        if (record.factoryId) {
          this.workshopStatus = false
          this.workshopDictCode = 'mes_base_factory,factory_name,id,factory_level=2 and factory_status=1 and pid=\'' + record.factoryId + '\''
        }
        if (record.workshopId) {
          this.lineStatus = false
          this.lineDictCode = 'mes_base_factory,factory_name,id,factory_level=3 and factory_status=1 and pid=\'' + record.workshopId + '\''
        }
        this.model = Object.assign({}, record)
        this.visible = true
      },
      submitForm() {
        const that = this
        // 触发表单验证
        this.$refs.form.validate(valid => {
          if (valid) {
            that.confirmLoading = true
            let httpurl = ''
            let method = ''
            if (!this.model.id) {
              httpurl += this.url.add
              method = 'post'
            } else {
              httpurl += this.url.edit
              method = 'put'
            }
            httpAction(httpurl, this.model, method).then((res) => {
              if (res.success) {
                that.$message.success(res.message)
                that.$emit('ok')
              } else {
                that.$message.warning(res.message)
              }
            }).finally(() => {
              that.confirmLoading = false
            })
          }

        })
      }
    }
  }
</script>